package lex.proxyverfier.prefs;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.lex.swing.prefs.CategoryAdapter;

import lex.proxyverfier.ProxyVerifier;

public class VerificationCategory extends CategoryAdapter
{
	private JScrollPane scrollPane;
	private JTable table;
	private JPanel controlPane;
	private JButton removeButton;
	private JTextField textField;

	public static void main( String[] args )
	{
		JFrame frame = new JFrame();
		frame.getContentPane().add( new VerificationCategory() );
		frame.setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE );
		frame.pack();
		frame.setVisible( true );
	}

	public VerificationCategory()
	{
		super();
		initialize();
		this.hookActionListeners();
	}

	@Override
	public String getTitle()
	{
		return "Verifications";
	}

	@Override
	public void loadConfiguration()
	{}

	@Override
	public void saveConfiguration()
	{}

	private void hookActionListeners()
	{
		this.getTextField().addActionListener( new ActionListener(){
			@Override
			public void actionPerformed( ActionEvent e )
			{
				ProxyVerifier.getApplication().getVerifications().addURL( textField.getText() );
			}
		} );
		this.getTable().getSelectionModel().addListSelectionListener( new ListSelectionListener(){
			@Override
			public void valueChanged( ListSelectionEvent e )
			{
				int row = table.getSelectedRow();
				if( -1 == row )
				{
					getRemoveButton().setEnabled( false );
					getTextField().setText( "" );
					return;
				}
				else
				{
					getRemoveButton().setEnabled( true );
					getTextField().setText( String.valueOf( table.getValueAt( row, 0 ) ) );
					return;
				}
			}
		} );
		this.getRemoveButton().addActionListener( new ActionListener(){
			@Override
			public void actionPerformed( ActionEvent e )
			{
				int row = table.getSelectedRow();
				if( -1 == row )
					return;
				ProxyVerifier.getApplication().getVerifications().remove( row );
			}
		} );
	}

	private JTable getTable()
	{
		if( table == null )
		{
			table = new JTable( ProxyVerifier.getApplication().getVerifications() );
		}
		return table;
	}

	private void initialize()
	{
		BorderLayout thisLayout = new BorderLayout();
		thisLayout.setVgap( 5 );
		this.setLayout( thisLayout );
		this.add( getScrollPane(), BorderLayout.CENTER );
		this.add( getControlPane(), BorderLayout.NORTH );
	}

	private JScrollPane getScrollPane()
	{
		if( scrollPane == null )
		{
			scrollPane = new JScrollPane();
			scrollPane.setViewportView( getTable() );
		}
		return scrollPane;
	}

	private JPanel getControlPane()
	{
		if( controlPane == null )
		{
			controlPane = new JPanel();
			BorderLayout controlPaneLayout = new BorderLayout();
			controlPaneLayout.setHgap( 5 );
			controlPane.setLayout( controlPaneLayout );
			controlPane.add( getTextField(), BorderLayout.CENTER );
			controlPane.add( getRemoveButton(), BorderLayout.EAST );
		}
		return controlPane;
	}

	private JTextField getTextField()
	{
		if( textField == null )
		{
			textField = new JTextField();
		}
		return textField;
	}

	private JButton getRemoveButton()
	{
		if( removeButton == null )
		{
			removeButton = new JButton();
			removeButton.setText( "Remove" );
			removeButton.setEnabled( false );
		}
		return removeButton;
	}
}
